package com.ad.pet.app.main.view;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.ad.pet.R;
import com.ad.pet.common.data.utils.ui.ToastUtil;

/**忘记密码的请输入注册邮箱
 * Created by SEELE on 2018/9/29.
 */

public class InputEmailDialog extends Dialog {
    OnLotteryLinstener mOnLotteryLinstener;
    private int screenWidth;
    private EditText mEditText;


    public InputEmailDialog(Context context, int themeResId) {
        super(context, themeResId);
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        initView(context);
    }

    private void initView(Context context){
        View view = View.inflate(context, R.layout.layout_input_emal_dialog,null);
        final Button button =  view.findViewById(R.id.btnSure);
        mEditText = view.findViewById(R.id.et_content);
        View ivClose = view.findViewById(R.id.iv_close);
        setContentView(view);
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (screenWidth *  8/ 10); // 设置宽度
//        lp.height = (int)(lp.width*1.3);
        this.getWindow().setAttributes(lp);
        window.setGravity(Gravity.CENTER);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mEditText.getText().toString().trim();
                String p = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
                boolean isEmail = content.matches(p);
                if (!isEmail) {
                    ToastUtil.getToastUtil().showShort(getContext().getString(R.string.input_true_email));
                    return;
                }
                mOnLotteryLinstener.onSure(content);
            }
        });
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()>0){
                    button.setEnabled(true);
                }else {
                    button.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void setOnLotteryLinstener(OnLotteryLinstener onLotteryLinstener) {
        mOnLotteryLinstener = onLotteryLinstener;
    }

    public interface OnLotteryLinstener{
        void onSure(String email);
    }

    public EditText getEditText(){
        return  mEditText;
    }

    public void clearData(){
        mEditText.setText("");
    }
}
